﻿using DemoUser.Application.User.Dtos;
using DemoUser.Application.User.Interface;
using DemoUser.Core.User;

using Microsoft.Extensions.Logging;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoUser.Application.User.Services
{
    public class UserService : IUserService, ITransient
    {

        private readonly ISqlSugarClient _client;

        public UserService(ISqlSugarClient client)
        {
            _client = client;
        }

        //查询用户信息
        ///根据id查询用户信息
        public OUser SelectById(int id)
        {
            throw new NotImplementedException();
        }

        //根据用户查询用户信息
        public OUser SelectByUser(OUser user)
        {
            throw new NotImplementedException();
        }

        //根据用户名查询用户信息
        public OUser SelectByUsername(string username)
        {
            return _client.Queryable<OUser>().Single(user => user.Username == username);
        }

        //根据用户名和密码查询用户信息
        public OUser SelectByUsernameAndPassword(string username, string password)
        {
            throw new NotImplementedException();
        }

        //修改用户
        public OUser UpdateUser(OUser user)
        {
            throw new NotImplementedException();
        }

        //删除用户
        public bool DelUser(int id)
        {
            throw new NotImplementedException();
        }

        //新增用户
        public bool insertUser(OUser user)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 生成token
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string GenerateToken(string username, string password)
        {
            var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>()
            {
                {"username", username},
                {"password", password},

            });
            // 获取刷新 token
            var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, 1440); // 第二个参数是刷新 token 的有效期（分钟），当前设置1天

            // 设置响应报文头
            if (App.HttpContext != null)
            {
                App.HttpContext.Response.Headers["access-token"] = accessToken;
                App.HttpContext.Response.Headers["x-access-token"] = refreshToken;
            }

            return accessToken;
        }
    }
}
